lPlayer = FindMetaTable("Player")

function WeaponToModel( weapon_class )
	if (weapon_class == "weapon_357") then
		return "models/weapons/w_357.mdl"
	elseif (weapon_class == "weapon_crowbar") then
		return "models/weapons/w_crowbar.mdl" 
	elseif (weapon_class == "weapon_stunbaton") then
		return "models/weapons/w_stunbaton.mdl" 
	elseif (weapon_class == "weapon_pistol") then
		return "models/weapons/w_pistol.mdl"
	elseif (weapon_class == "weapon_smg1") then
		return "models/weapons/w_smg1.mdl"
	elseif (weapon_class == "weapon_crossbow") then
		return "models/weapons/w_crossbow.mdl" 
	elseif (weapon_class == "weapon_physcannon") then
		return "models/weapons/w_physics.mdl"
	elseif (weapon_class == "weapon_physgun") then
		return "models/weapons/w_superphyscannon.mdl"
	elseif (weapon_class == "weapon_rpg") then
		return "models/weapons/w_rocket_launcher.mdl"
	elseif (weapon_class == "weapon_shotgun") then
		return "models/weapons/w_shotgun.mdl"
	elseif (weapon_class == "weapon_ar2") then
		return "models/weapons/w_IRifle.mdl"
	elseif (weapon_class == "weapon_frag") then
		return "models/weapons/w_grenade.mdl"
	end
	Msg("Unknown weapon type: " .. weapon_class .. "\n")
end

function lPlayer:Inventory_SendUpdate()
	datastream.StreamToClients( self, "INVENTORY_UPDATE", { self.INVENTORY } )
end

function lPlayer:Inventory_AddItem( item )
	local new_inventory = string.Implode( ";", { self.INVENTORY , item } ) 
	self:Inventory_Save(new_inventory)
end

function lPlayer:Inventory_AddWeapon( weapon )
	//Msg("ADLEOAKDEAOKEADOEA: " .. self.INVENTORY)
	local l_string = string.Implode( ";", { self:Inventory_GetString() , weapon } )
	self:Inventory_Save(l_string)
end

function lPlayer:Inventory_RemoveItem( item_id )
	local inventory_table = self:Inventory_GetTable()
	
	local ent = ents.Create("weapon_dropped")
	ent:SetPos(self:GetEyeTrace().HitPos)
	ent.WeaponClass = inventory_table[item_id]
	ent.WeaponModel = WeaponToModel(inventory_table[item_id]) 
	ent:Spawn()
	
	table.remove(inventory_table, item_id)
	local inventory_string = table.concat(inventory_table, ";")
	self:Inventory_Save( inventory_string )

end

function lPlayer:Inventory_GetTable()
	local inventory_table = string.Explode( ";", self.INVENTORY )
	return inventory_table
end

function lPlayer:Inventory_GetString()
	Msg("INVENTORY_STRING = " .. self.INVENTORY .. "\n")
	return self.INVENTORY
end

function lPlayer:Inventory_AssignWeapons() //Namenjeno uporabi po vsaki spremembi INVENTORIJ-A
	self:StripWeapons()
	self:GiveJobWeapons()
	local inventory_table = self:Inventory_GetTable()
	for i=1, table.Count(inventory_table) do
		if (string.find(inventory_table[i], "weapon")) then
			self:Give(inventory_table[i])
		end
	end
end

function Stream_DropItem( ply, handler, id, encoded, decoded )
	ply:Inventory_RemoveItem(decoded)
end

datastream.Hook( "INVENTORY_DROP_ITEM", Stream_DropItem )
	
function lPlayer:Inventory_Save( inventory_string ) //Zelo prakticna funkcija, za hitro shranjevanje inventorija
	self.INVENTORY = inventory_string
	self:Inventory_AssignWeapons()
	self:Inventory_SendUpdate()
	self:SavePlayer()
end